package gonz;

import gonz.common.Config;
import gonz.common.DublicateChecker;
import gonz.common.PriceAndArticleParser;
import gonz.data.swim.Image;
import gonz.data.swim.TwoPiecesSwim;
import gonz.pages.swim.SwimIndexPage;
import gonz.pages.swim.SwimPage;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;

import java.io.File;
import java.util.List;


public class VS {
    public static final Logger log = Logger.getLogger(VS.class);
    public static final Logger outLog = Logger.getLogger("out");
    public static final String APP_NAME = "VSG";

    public static void main(String[] args) {
        log.info(APP_NAME + " has been started.");
        printHeader();
        WebDriver driver = setUpWebDriver();
        String grabStartFromLink = Config.settings.getString(Config.GRAB_START_FROM_KEY);
        List<String> grabDirectLinkList = Config.settings.getList(Config.GRAB_DIRECT_LINK_LIST_KEY);
        List<String> grabLinkList = Config.settings.getList(Config.GRAB_LIST_KEY);
        List<String> grabLinkImgFolderList = Config.settings.getList(Config.GRAB_LIST_IMG_FOLDER_KEY);
        String mainOutputDir = Config.settings.getString(Config.OUTPUT_DIR_KEY);
        if (StringUtils.isNotBlank(grabStartFromLink)) {
            grabStartFromDirectLink(driver, grabStartFromLink, "grab_start_from", mainOutputDir);
        } else if (!grabDirectLinkList.isEmpty()) {
            for (String grabDirectLink : grabDirectLinkList) {
                grabDirectLink(driver, grabDirectLink, "grab_direct_link", mainOutputDir);
            }
        } else if (!grabLinkList.isEmpty()) {
            for (int i = 0; i < grabLinkList.size(); i++) {
                String grabLink = grabLinkList.get(i);
                String imgFolder;
                if (grabLinkImgFolderList.size() != grabLinkList.size()) {
                    imgFolder = mainOutputDir;
                } else {
                    imgFolder = grabLinkImgFolderList.get(i);
                }
                String destFolder = mainOutputDir + File.separator + imgFolder;
                File folder = new File(destFolder);
                folder.mkdirs();
                grabStartFromGategoryLink(driver, grabLink, destFolder);
            }
        }
        driver.close();
        log.info(APP_NAME + " has been finished.");
    }


    private static WebDriver setUpWebDriver() {
        /*File profileDir = new File("profile/Selenium");
        FirefoxProfile profile = new FirefoxProfile(profileDir);
        //profile.addAdditionalPreferences(extraPrefs);
        WebDriver driver = new FirefoxDriver(profile);
        return driver;*/
        ProfilesIni allProfiles = new ProfilesIni();
        FirefoxProfile profile = allProfiles.getProfile("Selenium");
        //profile.setPreferences("foo.bar", 23);
        return new FirefoxDriver(profile);
    }

    private static void grabStartFromDirectLink(WebDriver driver, String startFromLink, String category, String imgFolder) {
        log.info("Grabbing from link: " + startFromLink);
        driver.get(startFromLink);
        SwimPage swimPage = new SwimPage(driver, imgFolder);
        printSwims(category, swimPage.getSwim(), imgFolder);

    }

    private static void grabStartFromGategoryLink(WebDriver driver, String categoryLink, String imgFolder) {
        log.info("Grabbing from category link: " + categoryLink + " to: " + imgFolder);
        SwimIndexPage swimIndexPage = new SwimIndexPage(driver, categoryLink);
        TwoPiecesSwim swim = swimIndexPage.openFirstSwim(imgFolder);
        printSwims(categoryLink, swim, imgFolder);

    }

    private static void grabDirectLink(WebDriver driver, String grabDirectLink, String category, String imgFolder) {
        log.info("Grabbing direct link: " + grabDirectLink);
        driver.get(grabDirectLink);
        SwimPage swimPage = new SwimPage(driver, imgFolder);
        printSwim(category, swimPage.getSwim());
    }

    private static void printHeader() {
        outLog.info(
                "Category, " +
                        "TwoPiecesSwim Number," +
                        "Link, " +
                        "Brand, " +
                        "Name, " +
                        "Top Solid Price, " +
                        "Top Prints Price, " +
                        "Top Price, " +
                        "Top Orig Price, " +
                        "Top Sale Price, " +
                        "Top Special Price, " +
                        "Top Clearance Price, " +
                        "Top Article, " +
                        "Local Images," +
                        "Bottom Solid Price, " +
                        "Bottom Prints Price, " +
                        "Bottom Price, " +
                        "Bottom Article");
    }


    private static void printSwims(String category, TwoPiecesSwim swim, String imgFolder) {
        while (!swim.getCurrentSwimNumber().equalsIgnoreCase(swim.getNextSwimNumber())) {
            //&& !swimPage.getCurrentSwimNumber().equalsIgnoreCase("10")

            if (!PriceAndArticleParser.UNKNOWN_SYMBOL.equalsIgnoreCase(swim.getTop().getArticle())
                    && DublicateChecker.isArticleDublExist(swim.getTop().getArticle())) {
                swim = swim.openNextSwim(imgFolder);
                continue;
            }
            printSwim(category, swim);
            swim = swim.openNextSwim(imgFolder);
        }

    }

    private static void printSwim(String category, TwoPiecesSwim swim) {
        String images = "";
        for (Image image : swim.getTop().getImages()) {
            images += image.getLocalName() + " ";
        }
        outLog.info(
                category +
                        ", " + swim.getCurrentSwimNumber() +
                        ", " + swim.getLink() +
                        ", " + swim.getBrandName() +
                        ", " + swim.getName() +
                        ", " + swim.getTop().getSolidsPrice() +
                        ", " + swim.getTop().getPrintsPrice() +
                        ", " + swim.getTop().getPrice() +
                        ", " + swim.getTop().getOrigPrice() +
                        ", " + swim.getTop().getSalePrice() +
                        ", " + swim.getTop().getSpecialPrice() +
                        ", " + swim.getTop().getClearancePrice() +
                        ", " + swim.getTop().getArticle() +
                        ", " + images +
                        ", " + swim.getBottom().getSolidsPrice() +
                        ", " + swim.getBottom().getPrintsPrice() +
                        ", " + swim.getBottom().getPrice() +
                        ", " + swim.getBottom().getArticle()
        );
    }
}
